package com.glaivelee.firstRefactor;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.glaivelee.abstracts.chooseType;

public class chooseType_Images extends chooseType {

	int ImagesCount = 1;

	public chooseType_Images(String srcValue, int imagesCount) {
		super(srcValue);
		this.ImagesCount = imagesCount;
	}

	public String exeRex() {
		String[] patternArr = {
		// "(src=\".*?\")",
		"(?<linkDel><a\\s{0,}([^>]+)>(?<linkContent>(.*?))</a>)" };
		String REPLACE = "";
		for (int j = 0; j < patternArr.length; j++) {

			// filter
			delNonStr(REPLACE);

			String filterStr = "";
			Pattern pattern = Pattern.compile(patternArr[j]);
			Matcher matcher = pattern.matcher(srcValue);
			// int count = 0;
			while (matcher.find()) {
				String linkDel = matcher.group("linkDel");
				String newValue = matcher.group("linkContent");

				System.out.println("linkDel=" + linkDel);
				System.out.println("newValue=" + newValue);
				
				filterStr = replaceByKey(patternArr[j], newValue);

				System.out.println(" ");
				System.out.println("filterStr=" + filterStr);

			}
			srcValue = filterStr;
		}
		return srcValue;
	}

	private String replaceByKey(String oldValue, String newValue) {
		Pattern p = Pattern.compile(oldValue, Pattern.UNIX_LINES);
		Matcher m = p.matcher(srcValue); 
		if (m.find()) {
			srcValue = m.replaceAll(newValue).trim();
		}

		return srcValue;
	}

	private void delNonStr(String REPLACE) {
		for (int i = 0; i < clearList.size(); i++) {
			Pattern p = Pattern.compile(clearList.get(i), 1);
			Matcher m = p.matcher(srcValue); 
			srcValue = m.replaceAll(REPLACE);
		}
	}

}
